package com.base.ane
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.StatusEvent;
	import flash.external.ExtensionContext;
	
	public class BaseExtension extends EventDispatcher
	{
		// 与java端中Map里的key一致
		public static const FUNCTION_INIT:String = "base_function_init";
		public static const FUNCTION_LOGIN:String = "base_function_login";
		public static const FUNCTION_PAY:String = "base_function_pay";
		public static const FUNCTION_EXIT:String = "base_function_exit";
		
		public static const EXTENSION_ID:String = "com.base.ane";//与extension.xml中的id标签一致
		private var extContext:ExtensionContext;
		
		/** 单例的实现 **/
		private static var _instance:BaseExtension;
		public function BaseExtension(target:IEventDispatcher=null)
		{
			super(target);
			if(extContext == null){
				extContext = ExtensionContext.createExtensionContext(EXTENSION_ID,"");
				extContext.addEventListener(StatusEvent.STATUS,statusHandler);
			}
		}
		
		// 第二个为参数，会传入java代码中的FREExtension的createContext方法
		public static function getInstance():BaseExtension{
			if(_instance == null) _instance = new BaseExtension();
			return _instance;
		}
		
		// 转抛事件
		private function statusHandler(event:StatusEvent):void{
			dispatchEvent(event);
		}
		
		/**
		 *初始化发送函数  
		 * @param key 暂时传什么都可以  留着可能要用
		 * @return 
		 * 
		 */
		public function FuncInit(key:int):String{
			if(extContext){
				return extContext.call(FUNCTION_INIT,key) as String;
			}
			return "call init failed";
		}
		
		public function FuncLogin(key:int):String{
			if(extContext ){
				return extContext.call(FUNCTION_LOGIN,key) as String;
			}
			return "call login failed";
		} 
		
		public function FuncPay(data:Vector.<String>,len:int = 5):String{
			if(extContext && data.length == len){ 
				return extContext.call(FUNCTION_PAY,data)as String;
			}
			return "call pay failed";
		}
		
		public function ExitSDKHandle(key:int):String{
			if(extContext){ 
				return extContext.call(FUNCTION_EXIT,key) as String;
			}
			return "call exit failed";
		}
	}
}